﻿using FinalProject.Data;
using FinalProject.Data.Model;
using FinalProjectMVC.Adapters.Interfaces;
using FinalProjectMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FinalProjectMVC.Adapters.Adapters
{
    public class PhotosAdapter : IPhotosAdapter
    {
        public List<PhotosVM> GetAllPhotos()
        {
            List<PhotosVM> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Photos.Where(x => x.DateDeleted == null).Select(x => new PhotosVM()
                 {
                     PhotoID = x.PhotoID,
                     FilePath = x.FilePath,
                     Description = x.Description
                 }).ToList();
            }
            return model;
        }

        public PhotosVM CRUDForPhoto(int id)
        {
            PhotosVM model = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Photos.Where(x => x.PhotoID == id).Select(x => new PhotosVM()
                {
                    Description = x.Description,
                    FilePath = x.FilePath,
                    PhotoID = x.PhotoID,
                    EventId = x.EventId
                }).FirstOrDefault();
            }
            return model;
        }

        public Photo EditPhoto(PhotosVM photo)
        {
            Photo model = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Photos.FirstOrDefault(x => x.PhotoID == photo.PhotoID);
                model.PhotoID = photo.PhotoID;
                model.FilePath = photo.FilePath;
                model.Description = photo.Description;
                model.EventId = photo.EventId;
                db.SaveChanges();
            }
            return model;
        }

        public Photo AddPhoto(PhotosVM photo)
        {
            Photo model = new Photo();
            model.PhotoID = photo.PhotoID;
            model.FilePath = photo.FilePath;
            model.Description = photo.Description;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Photos.Add(model);
                db.SaveChanges();
            }
            return model;
        }

        public PhotosVM GetPhoto(int id)
        {
            PhotosVM model = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Photos.Where(x => x.PhotoID == id).Select(x => new PhotosVM()
                {
                    Description = x.Description,
                    PhotoID = x.PhotoID,
                    EventId = x.EventId,
                    FilePath = x.FilePath
                }).FirstOrDefault();
            }
            return model;
        }

        public void DeletePhoto(int id)
        {
            Photo model = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Photos.FirstOrDefault(x => x.PhotoID == id);
                model.DateDeleted = DateTime.Now;
                db.SaveChanges();
            }
        }

        public List<EventVM> GetAllEvents()
        {
            List<EventVM> events = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                events = db.Events.Where(x => x.DateDeleted == null).Select(x => new EventVM()
                {
                    DateNTIme = x.DateNTime,
                    City = x.City,
                    Comments = x.Comments.Select(c => new CommentsVM()
                    {
                        CommentBody = c.CommentBody,
                        CommentID = c.CommentID,
                        DateAdded = c.DateAdded,
                        UserName = c.User.UserName,
                        EventID = c.EventId,
                    }).ToList(),
                    EventId = x.EventId,
                    State = x.State,
                    Venue = x.Venue
                }).ToList();
            }
            return events;
        }

        public void CreateComment(CommentsVM model)
        {
            Comment comment = new Comment();
            comment.CommentBody = model.CommentBody;
            comment.DateAdded = DateTime.Now;
            comment.EventId = model.EventID;
            comment.UserId = model.UserId;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Comments.Add(comment);
                db.SaveChanges();
            }
        }

        public CommentsVM GetComment(int id)
        {
            CommentsVM model = new CommentsVM()
            {
                EventID = id
            };
            return model;
        }
    }
}